package com.qkbb.admin.kuibu.qkbb.funcation;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.ObjectMetadata;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qkbb.admin.kuibu.qkbb.url.Url;
import com.qkbb.admin.kuibu.qkbb.view.CustomProgressDialog;

/**
 * Created by admin on 2016/2/5.
 */
public class OSShelp {
    private Context context;
    private OSS oss;
    private String url;
    private String user_token;
    private String mdurl;
    private CustomProgressDialog progressDialog;

    public OSShelp(Context context) {
        this.context = context;
    }

    public void intitoss(){
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        OSSCredentialProvider ossCredentialProvider= new OSSPlainTextAKSKCredentialProvider("alEG52vgZVycqVE4","cettwWPmVP3M13IuHMfsrUiQceeWQj");
        ClientConfiguration conf=new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求书，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次
        oss=new OSSClient(context,endpoint,ossCredentialProvider);
    }
    public String getBitmap(String imagename){
        intitoss();

        String url = null;
        try {
            url = oss.presignConstrainedObjectURL("keeboo-userphoto", imagename, 60 * 60*24);
            Log.e("url",url);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return url;
    }
    public void putPhoto(String imagepath, final CustomProgressDialog progressDialog){
        this.progressDialog=progressDialog;
        progressDialog.setTitle("头像上传中");
        progressDialog.show();
        intitoss();

        mdurl=MD5Utils.md5(imagepath);
        PutObjectRequest put = new PutObjectRequest("keeboo-userphoto", "cettwWPmVP3M13IuHMfsrUiQceeWQj", imagepath);
//        ObjectMetadata objectMetadata=new ObjectMetadata();
//        objectMetadata.setContentDisposition("sagre_2");
//        objectMetadata.setContentType("image/png");
//        put.setMetadata(objectMetadata);

        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
                Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);

            }
        });
        put.setObjectKey(mdurl);
        Log.e("durl",mdurl);
        OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
//                Log.e("PutObject", "UploadSuccess");
                Log.e("123",request.getObjectKey());
                putPhotostring();
                Toast.makeText(context,"上传成功",Toast.LENGTH_SHORT).show();
                progressDialog.cancel();

            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                // 请求异常
                if (clientExcepion != null) {
                    // 本地异常如网络异常等
                    clientExcepion.printStackTrace();
                }
                if (serviceException != null) {
                    // 服务异常
                    Log.e("ErrorCode", serviceException.getErrorCode());
                    Log.e("RequestId", serviceException.getRequestId());
                    Log.e("HostId", serviceException.getHostId());
                    Log.e("RawMessage", serviceException.getRawMessage());
                }
            }
        });
    }
    private void putPhotostring(){
        HttpUtils httpUtils=new HttpUtils();
        RequestParams params=new RequestParams();
        params.addBodyParameter("photo",mdurl);

        user_token=SDCardHelper.loadFileFromSdCard("user_token",context);
        httpUtils.send(HttpRequest.HttpMethod.PUT, Url.PUTPHOTO+user_token, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
    }
}
